package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.SelfClass.ResponseOrderItem;
import com.qf.SelfClass.ResponseOrderSon;
import com.qf.pojo.*;
import com.qf.service.CarService;
import com.qf.service.CityService;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Liu
 * @date 2021/12/16 20:23
 */
@RequestMapping("/item")
@Controller
public class OrderItemController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CarService carService;
    @Autowired
    private CityService cityService;
    /**
     * 完成订单项
     * @return
     */
    @ResponseBody
    @RequestMapping("/orderItem")
    public ResponseOrderItem findItem(@RequestParam("page") String page, HttpServletRequest request){
        //System.out.println(page);
        User user = (User) request.getSession().getAttribute("user");
        Integer uid = user.getId();//获取用户id
        int pageNum = Integer.parseInt(page);//获取当前页码
        String pageSize1 = request.getParameter("pageSize");//获取每页显示的条数
        if(pageSize1==null){
            pageSize1="5";
        }
        int pageSize = Integer.parseInt(pageSize1);
        ResponseOrderSon ros = new ResponseOrderSon();
        List<OrderList> list = new ArrayList<>();
        Integer allTotal = orderService.findTotalSize(uid);//查询订单总数
        ros.setTotal(allTotal);
        List<Order> orders = orderService.findAllOrder(pageNum,pageSize,uid);//查询所有订单
        for(Order order : orders){
            OrderList orderList = new OrderList();
            Integer cid = order.getCid();
            Car car = carService.findCarById(cid);//查询车辆信息
            String carType = car.getType();//车型
            //System.out.println(car);
            Integer getid = order.getGetid();
            Integer backid = order.getBackid();
            City getCity = cityService.findCityByGetid(getid);//查询取车城市
            String getName = getCity.getName();//取车地点
            City backCity = cityService.findCityByBackid(backid);//查询还车城市
            String backName = backCity.getName();//还车地点
            /*System.out.println(getCity);
            System.out.println(backCity);*/
            orderList.setOid(order.getId());
            orderList.setOprice(order.getOprice());
            orderList.setCname(carType);
            orderList.setGetcity(getName);
            orderList.setBackcity(backName);
            orderList.setOstatus(order.getStatus());
            list.add(orderList);
        }
        PageInfo<OrderList> pages = PageInfo.of(list);//封装到pageInfo中
        //System.out.println(pages);
        ros.setRows(list);
        ResponseOrderItem roi = new ResponseOrderItem();
        roi.setInfo(ros);
        roi.setCode(1);
        if(list.size()!=0){
            return roi;
        }
        return new ResponseOrderItem(2,"暂无订单,快去添加");
    }
}
